<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>题库管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>组件</cite></a>
    <a><cite>数据表格</cite></a>
    <a><cite>题库管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">题库管理</div>
        <div class="layui-card-body">

          <div class="test-table-reload-btn layui-form-item" style="margin-bottom: 10px;">
            用户姓名：
            <div class="layui-inline">
              <input class="layui-input" name="expertName" autocomplete="off">
            </div>
            登录账号：
            <div class="layui-inline">
              <input class="layui-input" name="username" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
          </div>

          <table class="layui-hide" id="table-expert" lay-filter="test-table-toolbar"></table>

          <script type="text/html" id="test-table-public">
            {{#  if(d.isPublic == 0){ }}
            <input type="checkbox" value="{{ d.bankId }}" name="isPublic" lay-skin="switch" lay-text="公开|私有" lay-filter="test-table-public">
            {{#  } else { }}
            <input type="checkbox" value="{{ d.bankId }}"  name="isPublic" lay-skin="switch" lay-text="公开|私有" lay-filter="test-table-public" checked="checked">
            {{#  } }}
          </script>
          <script type="text/html" id="templet-expertPassword">
            {{d.expertPassword}}
          </script>


          <script type="text/html" id="templet-expertSex">
            {{#  if(d.expertSex == 0){ }}
            <span class="layui-badge layui-bg-blue">男</span>
            {{#  } else { }}
            <span class="layui-badge layui-bg-orange">女</span>
            {{#  } }}
          </script>

          <script type="text/html" id="table-expert-toolbar">
            <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm" lay-event="add">新增专家</button>
            </div>
          </script>

          <script type="text/html" id="table-expert-list">
            <a class="layui-btn layui-btn-xs" lay-event="refresh"><i class="layui-icon layui-icon-refresh-3"></i>重置密码</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
          </script>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    editData = [];
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var admin = layui.admin
            ,table = layui.table
            ,$ = layui.$
            ,form = layui.form;

        table.render({
            elem: '#table-expert'
            ,url: '/admin/expert/experts'
            ,page: true
            ,limit : 10
            ,limits: [10,30,60,90]
            ,loading: true   //开启翻页加载动画
            ,toolbar: '#table-expert-toolbar'   //开启toolbar
            //,height: 'full-100' //高度最大自适应
            ,cols: [[
                {field:'expertId',  title: 'ID', sort: true ,hide:true}
                ,{field:'expertName',  title: '姓名'}
                ,{field:'expertPhone',  title: '手机号'}
                ,{field:'expertSex',width:80, title: '性别',align:'center',unresize: true,templet: '#templet-expertSex'}
                ,{field:'expertEmail',  title: '邮箱'}
                ,{field:'expertUsername',  title: '登录账号'}
                ,{field:'expertPassword',  title: '密码', hide:true, templet: '#templet-expertPassword'}
                ,{field:'roleName',  title: '角色'}
                ,{title: '操作',width:250,templet: '#table-expert-list',align:'center',fixed:'right'}
            ]]

        });

        //头工具栏事件
        table.on('toolbar(test-table-toolbar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            if(obj.event === 'add'){
                expertAdd();
            }
        });

        //监听行工具事件
        table.on('tool(test-table-toolbar)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('删除后数据不可恢复，确定要删除么？', function(index){
                    console.log(data.expertId);
                    $.ajax({
                        url:"/admin/expert/expert/"+data.expertId,
                        async:false,
                        method:"delete",
                        dataType:"json"
                    });
                    table.reload('table-expert');
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                editData = data;
                expertEdit();
            } else if(obj.event === 'refresh'){
                layer.confirm('重置密码会覆盖原先密码，请慎重操作', function(index){
                    console.log(data.expertId);
                    $.ajax({
                        url:"/admin/expert/password/"+data.expertId,
                        async:false,
                        method:"put"
                    });
                    table.reload('table-expert');
                    layer.close(index);
                    layer.msg("重置密码成功,初始密码为123456");
                });
            }
        });

        //监听公开操作
        form.on('switch(test-table-public)', function(obj){
            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });

        //搜索
        var active = {
            reload: function(){
                var expertName = $('[name=expertName]').val();
                var username = $('[name=username]').val();
                //执行重载
                table.reload('table-expert', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        expertName: expertName,
                        username: username,
                    }
                });
            }
        };

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

    function expertAdd(){
        layer.open({
            type: 2
            ,title: '添加题库'
            ,content: '/layuiadmin/add/expert-add.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
    }

    function expertEdit(){
        layer.open({
            type: 2
            ,title: '修改题库'
            ,content: '/layuiadmin/edit/expert-edit.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
    }
</script>
</body>
</html>